package com.andcard;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TabHost;
import android.util.Log;

import com.andcard.action.TabArrange;
import com.andcard.action.TabCard;
import com.andcard.action.TabConfig;
import com.andcard.action.TabStats;
import com.andcard.sql.DbAdapter;
import com.andcard.util.Reference;

public class AndCard extends TabActivity implements TabHost.OnTabChangeListener{
	
	private TabCard tabCard;
	private TabConfig tabConfig;
	private TabStats tabStats;
	private TabArrange tabArrange;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	Log.d(Reference.TAG, "AndCard.onCreate");
        setContentView(R.layout.main);

        TabHost mTabHost = getTabHost();
        
        mTabHost.addTab(mTabHost.newTabSpec("tabCards").setIndicator("Cards").setContent(R.id.tabCards));
        mTabHost.addTab(mTabHost.newTabSpec("tabConfig").setIndicator("Config").setContent(R.id.tabConfig));
        mTabHost.addTab(mTabHost.newTabSpec("tabDBStats").setIndicator("DB Stats").setContent(R.id.tabDBStats));
        mTabHost.addTab(mTabHost.newTabSpec("tabArrange").setIndicator("Arrange").setContent(R.id.tabArrange));
        // listener for tab
        mTabHost.setOnTabChangedListener(this);
        
        mTabHost.setCurrentTab(0);
        
        // DbAdapter
        DbAdapter dbAdapter = new DbAdapter();
        tabCard = new TabCard(this, dbAdapter);
        tabConfig = new TabConfig(this, dbAdapter);
        tabStats = new TabStats(this, dbAdapter);
        tabArrange = new TabArrange(this, dbAdapter);

    }
    
    // end application
    public void onDestroy(){
    	Log.d(Reference.TAG, "AndCard.onDestroy");
    	super.onDestroy();
    }

    protected Dialog onCreateDialog(int id) {

    	Log.d(Reference.TAG, "AndCard.onCreateDialog -id " + id);
    	
    	Dialog dialog = null;
    	// reference to me
    	// we need it follow listeners
    	final AndCard andCard = this;
    	
        if (id < Reference.MAX_DIALOG_ALERT){
        	
        	Log.d(Reference.TAG, "AndCard.onCreateDialog -ALERT_DIALOG");
        	
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setMessage(Reference.ALERT_MESSAGES[id]);
            builder.setCancelable(false);
            
            builder.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
        	// create dialog            
            dialog = builder.create();
        }else if(id == Reference.ALERT_ACTION_DELETE_CARD){
        	
        	Log.d(Reference.TAG, "AndCard.onCreateDialog -ALERT_DELETE_CARD_DIALOG");
        	
        	AlertDialog.Builder builder = new AlertDialog.Builder(this);
        	builder.setMessage(Reference.ALERT_ACTION_MESSAGES[id - Reference.MAX_DIALOG_ALERT]);
        	builder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            	public void onClick(DialogInterface dialog, int id) {
                    andCard.tabCard.deleteCard();
               }
        	});
        	builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            	public void onClick(DialogInterface dialog, int id) {
                     dialog.cancel();
                }
            });
        	// create dialog
        	dialog = builder.create();
        }
        return dialog;
    }
    
    public void onTabChanged(String tabId){
 
    	Log.d(Reference.TAG, "AndCard.onTabChanged -tabId: " + tabId);
    	
    	if (tabId.equals("tabArrange")){
    		tabArrange.activeTab(tabCard.getCounter());
    	}
    }
    
    public TabCard getTabCard(){
    	return tabCard;	
    }
    
    public TabConfig getTabConfig(){
    	return tabConfig;
    }
    
    public TabStats getTabStats(){
    	return tabStats;
    }
    
    public TabArrange getTabArrange(){
    	return tabArrange;
    }

}